﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weight.Model;
using Weight.Model.ParamEntity;
using Dapper;
using System.Data;
using Weight.Base;
using Dapper.Contrib;
using Dapper.Contrib.Extensions;

namespace Weight.Dal.Contract
{
   public class DalContractCustomer
    {
        /// <summary>
        /// 获取订单客户信息列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
       public IEnumerable<ContractCustomerEntity> QueryList(QueryContractCustomerReq req)
        {
            using(IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = "select * from ContractCustomer ";
                sql += " where IsDel=@IsDel ";
                if (!string.IsNullOrEmpty(req.CustomerName))
                {
                    req.CustomerName = "%" + req.CustomerName + "%";
                    sql += " and Customer like @CustomerName";
                }
                if(!string.IsNullOrEmpty(req.IsValid))
                {
                    sql += " and IsValid=@IsValid";
                }
                if(!string.IsNullOrEmpty(req.IsVip))
                {
                    sql += " and IsVip=@IsVip";
                }
                sql += " order by CreateDate desc ";
               return   conn.Query<ContractCustomerEntity>(sql,req);
            }
        }

        /// <summary>
        /// 根据Id查找订单客户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ContractCustomerEntity GetById(string id)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = "select * from ContractCustomer ";
                sql += " where IsValid=@IsValid  and Id=@Id";
                return conn.Query<ContractCustomerEntity>(sql,new { IsValid="是",Id = id }).FirstOrDefault();
            }
        }

        /// <summary>
        /// 添加订单客户信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Add(ContractCustomerEntity entity)
        {
            using(IDbConnection conn = SessionFactory.CreateConnection())
            {
                long res = conn.Insert(entity);
                return true;
            }
        }


        /// <summary>
        /// 判断客户信息是否存在
        /// </summary>
        /// <param name="cardId"></param>
        /// <returns></returns>
        public bool IsExist(string cardId)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = "select * from ContractCustomer ";
                sql += " where IsDel=@IsDel  and CardId=@CardId";
                return conn.Query<ContractCustomerEntity>(sql, new { IsDel = "否", CardId = cardId }).ToList().Count > 0;
            }
        }

        /// <summary>
        /// 判断客户信息是否存在
        /// </summary>
        /// <param name="cardId"></param>
        /// <returns></returns>
        public bool IsExist(string cardId,string id)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = "select * from ContractCustomer ";
                sql += " where IsDel=@IsDel  and CardId=@CardId  and Id<>@Id";
                return conn.Query<ContractCustomerEntity>(sql, new { IsDel = "否", CardId = cardId ,Id=id}).ToList().Count > 0;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(ContractCustomerEntity entity)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                return  conn.Update(entity);
            }
        }

        /// <summary>
        /// 设置是否有效
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isValid"></param>
        /// <returns></returns>
        public bool SetValid(string id,bool isValid)
        {
            string state = isValid ? "是" : "否";
            string sql = "update ContractCustomer set IsValid=@IsValid where Id=@Id";
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                return conn.Execute(sql,new { Id=id, IsValid= state }) > 0 ;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Del(string id)
        {
            string sql = "update ContractCustomer set IsDel=@IsDel where Id=@Id";
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                return conn.Execute(sql, new { Id = id, IsDel = false }) > 0;
            }
        }
    }
}
